import {AxiosRequestConfigs, AxiosResponse} from "@/types";

export class AxiosError extends Error {
    isAxiosError: boolean
    config: AxiosRequestConfigs
    code?: string | null
    request?: any
    response?: AxiosResponse

    constructor(
        message: string,
        config: AxiosRequestConfigs,
        code ?: string | null,
        request?: any,
        response?: AxiosResponse) {
        super(message);
        this.config = config;
        this.code = code;
        this.request = request;
        this.response = response;
        this.isAxiosError = true;
        Object.setPrototypeOf(this, AxiosError.prototype)
    }
}

/**
 * 工厂方法创建error
 * @param message
 * @param config
 * @param code
 * @param request
 * @param response
 */
export function createError(
    message: string,
    config: AxiosRequestConfigs,
    code ?: string | null,
    request?: any,
    response?: AxiosResponse
) {
    const error = new AxiosError(message, config, code, request, response)
    return error;
}